package com.wanmait.recycle.webcontroller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wanmait.recycle.pojo.Article;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Question;
import com.wanmait.recycle.service.ArticleService;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.QuestionService;
import com.wanmait.recycle.util.Pager;
import com.wanmait.recycle.util.RemoveHtmlTagUtil;

@Controller
public class WebSolrController {

	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private QuestionService questionService;
	@Resource
	private ArticleService articleService;
	
	@RequestMapping("web/question/result")
	public void result() {
		
	}

	@RequestMapping("web/seachQuestion")
	public ModelAndView resultQuestion(String keyword,Integer pageNo) {
		
		ModelAndView mv = new ModelAndView();
		
		if(keyword==null || keyword.trim().equals(""))
		{
			mv.setViewName("redirect:/web/question/list");
			return mv;
		}
		Pager pager = new Pager();
		if(pageNo!=null)
		{
			
			pager.setPageNo(pageNo);
			
			
		}
		
	
		List<Question> questions = questionService.seachBySolr(keyword,pager);
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		
		mv.addObject("questions", questions);
		mv.addObject("pager",pager);
		mv.addObject("keyword",keyword);
		mv.setViewName("/web/question/result");
		
		return mv;
		
	}
	
	@RequestMapping("web/seachArticle")
	public ModelAndView resultArticle(String keyword,Integer pageNo) {
		
		ModelAndView mv = new ModelAndView();
		
		if(keyword==null || keyword.trim().equals(""))
		{
			mv.setViewName("redirect:/index");
			return mv;
		}
		Pager pager = new Pager();
		if(pageNo!=null)
		{
			
			pager.setPageNo(pageNo);
			
			
		}
		
		pager.setPageSize(5);
		List<Article> articles = articleService.seachBySolr(keyword,pager);
		for (Article article : articles) {
			article.setContent(RemoveHtmlTagUtil.htmlRemoveTag(article.getContent()));
		}
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		
		mv.addObject("articles", articles);
		mv.addObject("pager",pager);
		mv.addObject("keyword",keyword);
		mv.setViewName("/web/article/articleResult");
		
		return mv;
		
	}
	
	@RequestMapping("web/article/articleResult")
	private void articleResult() {
		
	}
	
}
